package org.dftproject.genesis.ui.pages.summary.details;

import org.dftproject.genesis.ui.figures.ColumnData;
import org.dftproject.genesis.ui.figures.ColumnLayout;
import org.dftproject.genesis.ui.figures.UniformSizeContainer;
import org.dftproject.genesis.ui.figures.UniformSizeGroup;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseMotionListener;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;

public class DetailFigure extends Figure {
	
	protected final Font boldFont = JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT);
	protected final Color hoverColor = new Color(null, 236, 236, 255);
	protected final int inset = 10;
	
	private boolean hover;
	
	public DetailFigure(String name, IFigure value, UniformSizeGroup uniformSizeGroup) {
		addListeners();
		
		setLayoutManager(new ColumnLayout(10));
		
		Label label = new Label(name);
		label.setLabelAlignment(PositionConstants.RIGHT);
		label.setFont(boldFont);
		add(new UniformSizeContainer(label, uniformSizeGroup), new ColumnData());
		
		add(value, new ColumnData(100));
	}

	protected void addListeners() {
		MouseTracker tracker = new MouseTracker();
		addMouseMotionListener(tracker);
	}
	
	protected void setHover(boolean hover) {
		this.hover = hover;
	}
	
	@Override
	public void paint(Graphics graphics) {
		if (false && hover) {
			graphics.pushState();
			graphics.setAntialias(SWT.ON);
			graphics.setBackgroundColor(hoverColor);
			graphics.fillRoundRectangle(getBounds(), inset, inset);
			graphics.popState();
		}

		super.paint(graphics);
	}
	
	protected class MouseTracker extends MouseMotionListener.Stub {
		
		@Override
		public void mouseEntered(MouseEvent me) {
			setHover(true);
			repaint();
		}
		
		@Override
		public void mouseExited(MouseEvent me) {
			setHover(false);
			repaint();
		}

	}

}
